package com.gitee.ywj1352.bootDemo;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.GenericApplicationContext;

/**
 * Created by wenjie.yang on 2019/7/13.
 */
public class EventListenerDemo {


    public static void main(String[] args) {
        GenericApplicationContext app = new GenericApplicationContext();


        app.addApplicationListener(event -> {
            System.out.println("监听事件" + event);
        });

        //启动上下文
        app.refresh();
        // spring 事件都是 application_event
        //spring 应用上下文发布事件
        app.publishEvent("hello world");
    }


    //自定义监听类
    private static class MyListenerEvenet implements ApplicationListener<ContextClosedEvent> {
        @Override
        public void onApplicationEvent(ContextClosedEvent event) {

            System.out.println("监听事件" + event);

        }
    }
}
